package basic.lessons.mosaic;

public class MosaicStrobe {
    
    /* This program shows an animation in which a small square grows
       from the center of the window until it fills the whole square.
       Then, after a short delay, the process repeats.  As the square grows,
       its brightness also increases.  This program depends on the
       non-standard classes Mosaic and MosaicCanvas.
    */

  public static void main(String[] args) {
         // Open a Mosaic window, then show the "strobe" animation over
         // and over until the user closes the window.
     Mosaic.open(41,41,5,5);
     while ( Mosaic.isOpen() ) {
         Mosaic.delay(1000);
         strobe();
     }
  }  // end main()

  
  static void strobe() {
          // Draw the animation, showing a square that starts at
          // the center of the mosaic and grows to fill the whole window.
          // The square gets brighter as it grows.  Note that the
          // animation ends immediately if the user closes the window.

     int rectSize;    // The number of rows (and columns) in the square.
     int left;        // The leftmost column in the square.
     int top;         // The topmost row in the square.
     int brightness;  // The brightness of the square, which increases from
                      //   50 to a maximum of 250 as the square grows.  This
                      //   quantity is used for all three color components,
                      //   giving a gray color that brightens to white.

     left = 20;       // Start at the center of the 41-by-41 mosaic.
     top = 20;
     
     rectSize = 1;  
     brightness = 50;

     while (left >= 0 && Mosaic.isOpen()) {
            // Draw the square in gray, pause so the user can see it, then
            // redraw the same rectangle in black, effectively erasing the
            // gray square.
         rectangle(top,left,rectSize,rectSize,brightness,brightness,brightness);
         Mosaic.delay(50);
         rectangle(top,left,rectSize,rectSize,0,0,0);
            // Now, adjust the parameters to get ready to draw the next square
         left--;
         top--;
         rectSize += 2;
         brightness += 10;
     }
     
  } // end strobe()
  

  static void rectangle(int top, int left, int height, int width, int r, int g, int b) {
        // Draws the outline of a rectangle in the mosaic window by setting
        // the color of each little square on that rectangle;  top gives the
        // starting row of the rectangle;  left gives the starting column;
        // height gives the number of rows in the rectangle;  width is the
        // number of columns.  The red, green, and blue components of the 
        // color are specified by r, g, and b.
     int row, col;
     for ( row = top;  row < top + height;  row++ ) {
        Mosaic.setColor(row, left, r, g, b);
        Mosaic.setColor(row, left + width - 1, r, g, b);
     }
     for ( col = left;  col < left + width; col++ ) {
        Mosaic.setColor(top, col, r, g, b);
        Mosaic.setColor(top + height - 1, col, r, g, b);
     }
  }  // end rectangle()

}  // end class MosaicStrobe
